home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 July: Mac OS SDK / Dev.CD Jul 97 SDK1.toast / Development Kits (Disc 1) / Apple Guide / Engineering / Context Check Modules / Standard CC Modules / Utility.c < prev    next >
Encoding:
Text File  |  1994-04-25  |  5.0 KB  |  242 lines  |  [TEXT/MPS ]

  1. //    Copyright:    © 1993 Apple Computer, Inc. All rights reserved.
  2. //    Author:        Scott Searle (original)
  3. //                    Victor J. Hnyp (extensions)
  4. //    Date:            03/13/93
  5.  
  6. // Revisions
  7. //
  8. //    03/13/93    VJH    2.06    Fixed the way string compare works in EQString
  9. //
  10. //    03/11/93    VJH    2.05    Fixed the way string compare "matches" works
  11. //
  12. //    03/11/93    VJH    2.04    Changes to the way string compare "matches" works
  13. //
  14. //    02/08/93    VJH    2.03    Changes to the way GetNextItemAddress works
  15. //
  16. //    01/18/93    VJH    2.02    Added:    N2S and GetNextItemAddress
  17. //
  18. //    01/04/93    VJH    2.01                Changed Copyright Notice, added new functions
  19.  
  20. #include "AllHeaders.h"
  21. #include "Utility.h"
  22. #include "Context.h"
  23.  
  24. //            UTILITY PROTOTYPES
  25. OSErr        SetContextResult(void* theData, Size theSize, void* outMessage, Size* outSize);
  26. Boolean    CompareStringSpec(unsigned char* s1, StringSpecPtr stringSpec);
  27. Boolean    EQString(register unsigned char* s1, register unsigned char* s2);
  28. Boolean    TrapAvailable(short theTrap);
  29. Boolean    StrStartsWith(unsigned char* s1, unsigned char* s2);
  30. Boolean    StrEndsWith(unsigned char* s1, unsigned char* s2);
  31. Boolean    StrContains(unsigned char* targetStr, unsigned char* searchStr);
  32. OSErr     GetTheString(short id, unsigned char* str);
  33. StringPtr N2S( const long n );
  34. unsigned char* GetNextItemAddress( StringPtr theString );
  35.  
  36. OSErr SetContextResult(void* theData, Size theSize, Ptr* outMessage, Size* outSize)
  37. {
  38.     Ptr    p;
  39.     
  40.     if (p = NewPtr(theSize))
  41.     {
  42.         BlockMove(theData, p, theSize);
  43.         
  44.         *outSize        =    theSize;
  45.         *outMessage    =    p;
  46.         
  47.         return(noErr);
  48.     }
  49.     else
  50.         return(MemError());
  51. }
  52.  
  53. Boolean CompareStringSpec(unsigned char* s1, StringSpecPtr stringSpec)
  54. {
  55.     Str255            test1, test2;
  56.     unsigned char    i;
  57.  
  58.     BlockMove(s1, test1, (long)(*s1 + 1));
  59.     BlockMove(&stringSpec->str, test2, (long)(stringSpec->str[0] + 1));
  60.  
  61.     switch (stringSpec->method)
  62.     {
  63.         case    containsAnything:
  64.             return(*test1);
  65.             break;
  66.  
  67.         case    empty:
  68.             return(!*test1);
  69.             break;
  70.  
  71.         case    matches:
  72.             UprString(test1, false);
  73.             UprString(test2, false);
  74.             for (i = 0; i <= test1[0]; i++)
  75.                 if (test1[i] != test2[i])
  76.                     return( false );
  77.             return( true );
  78.             break;
  79.  
  80.         case    contains:
  81.             return(StrContains(test1, test2));
  82.             break;
  83.  
  84.         case    startsWith:
  85.             return(StrStartsWith(test1, test2));
  86.             break;
  87.  
  88.         case    endsWith:
  89.             return(StrEndsWith(test1, test2));
  90.             break;
  91.  
  92.         default:
  93.             return( false );
  94.             break;
  95.     }
  96. }
  97.  
  98. //    Compare s1 with s2; return true if they match exactly
  99. Boolean EQString(register unsigned char* s1, register unsigned char* s2)
  100. {
  101.     register    unsigned char    i;
  102.     Str255                        str1, str2;
  103.  
  104.     BlockMove(s1, str1, (long)(*s1 + 1));
  105.     BlockMove(s2, str2, (long)(*s2 + 1));
  106.  
  107.     UprString(str1, false);
  108.     UprString(str2, false);
  109.  
  110.     for (i = 0; i <= str1[0]; i++)
  111.         if (str1[i] != str2[i])
  112.             return(false);
  113.  
  114.     return(true);
  115. }
  116.  
  117. //    Returns true if the trap is implemented
  118. Boolean TrapAvailable(short theTrap)
  119. {
  120.     TrapType        tType = GetTrapType(theTrap);
  121.  
  122.     if (tType == ToolTrap)
  123.     {
  124.         theTrap = theTrap & 0x07FF;
  125.  
  126.         if (theTrap >= NumToolboxTraps)
  127.             theTrap = _Unimplemented;
  128.     }
  129.  
  130.     return(NGetTrapAddress(theTrap, tType) != NGetTrapAddress(_Unimplemented, ToolTrap));
  131. }
  132.  
  133. //    True if s1 starts with s2
  134. Boolean StrStartsWith(unsigned char* s1, unsigned char* s2)
  135. {
  136.     register    unsigned char    i;
  137.  
  138.     UprString(s1, false);
  139.     UprString(s2, false);
  140.  
  141.     if (*s2 > *s1)
  142.         return(false);
  143.  
  144.     for (i = 1; i <= *s2; ++i)
  145.         if (s1[i] != s2[i])
  146.             return(false);
  147.  
  148.     return(true);
  149. }
  150.  
  151. //    True if s1 ends with s2
  152. Boolean StrEndsWith(unsigned char* s1, unsigned char* s2)
  153. {
  154.     UprString(s1, false);
  155.     UprString(s2, false);
  156.  
  157.     if (*s2 > *s1)
  158.         return(false);
  159.  
  160.     return(IUMagString(s1 + (*s1 - *s2) + 1, s2 + 1, (long)*s2, (long)*s2) == 0);
  161. }
  162.  
  163. //    True if targetStr contains searchStr
  164. Boolean StrContains(unsigned char* targetStr, unsigned char* searchStr)
  165. {
  166.     register    unsigned char    i, j, t;
  167.     register    Boolean            flag;
  168.  
  169.     UprString(targetStr, false);
  170.     UprString(searchStr, false);
  171.  
  172.     if (*targetStr < *searchStr)
  173.         return(false);
  174.  
  175.     for (i = 1; i <= *targetStr; ++i)
  176.     {
  177.         j = 1;
  178.         if (searchStr[j] == targetStr[i] && j <= *searchStr && i + (*searchStr - 1) <= *targetStr)
  179.         {
  180.             flag = true;
  181.             for (j = 2, t = i+1; j <= *searchStr && t <= *targetStr; ++j, ++t)
  182.                 if ((searchStr[j] != targetStr[t]))
  183.                 {
  184.                     flag = false;
  185.                     break;
  186.                 }
  187.  
  188.             if (flag)
  189.                 return(true);
  190.         }
  191.     }
  192.  
  193.     return(false);
  194. }
  195.  
  196.  
  197.  
  198. OSErr GetTheString(short id, unsigned char* str)
  199. {
  200.     StringHandle        sHdl;
  201.     short                oldRef    =    CurResFile();
  202.     OSErr                err        =    noErr;
  203.  
  204.     UseResFile(0);
  205.  
  206.     if (sHdl = Get1Resource('STR ', id))
  207.     {
  208.         BlockMove(*sHdl, str, (long)(**sHdl + 1));
  209.         ReleaseResource(sHdl);
  210.     }
  211.     else
  212.         err = ResError();
  213.  
  214.     UseResFile(oldRef);
  215.     return(err);
  216. }
  217.  
  218.  
  219.  
  220. StringPtr N2S( const long n )
  221. /*    Returns the string equivalent of a number */
  222. {
  223.     static Str255        theStr;
  224.     
  225.     NumToString( n, theStr );
  226.     return &theStr;
  227. } /* N2S */
  228.  
  229.  
  230.  
  231. unsigned char* GetNextItemAddress( StringPtr theString )
  232. /* Given a pointer to the first string in the message, returns a pointer
  233.     to the next Long (1st field following the 1st string in the message) */
  234. {
  235.     unsigned char*        aCharPtr;
  236.     
  237.     aCharPtr = theString + theString[0] + 1;
  238.     if( (long)aCharPtr % 2 )
  239.         aCharPtr += 1;
  240.     
  241.     return( aCharPtr );
  242. } // GetNextItemAddress